package com.linln.agent;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linln.common.constant.AdminConst;
import com.linln.common.enums.ResultEnum;
import com.linln.common.exception.ResultException;
import com.linln.common.utils.MD5;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.vo.ResultVo;
import com.linln.modules.member.domain.AreaCount;
import com.linln.modules.member.domain.MemberAgent;
import com.linln.modules.member.domain.MemberInfo;
import com.linln.modules.member.service.AreaCountService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Controller
@RequestMapping("/agent/areacount")
public class AgentAreaCountController {

    @Autowired
    private AreaCountService areaCountService;
     
   
    /**
     * 跳转到修改密码页面
     */
    @GetMapping("/toupdate")
     public String toUpdate(HttpServletRequest request,String area,Model model){
    	 MemberInfo agent=(MemberInfo)request.getSession().getAttribute(AdminConst.AGENT_ROLE_NAME); 
         
    	AreaCount areacount=areaCountService.findByArea(agent.getArea());
    	if(areacount==null) {
    		areacount=new AreaCount();
    		areacount.setArea(agent.getArea());
    		areacount.setMessage_shouFabunum(20000);
    		areacount.setMessage_shouReadnum(200000);
    		areacount.setMessage_shouShareunum(1000);
    		areaCountService.save(areacount);
    	}
        model.addAttribute("areacount", areacount);

        return "/agent/user/areacount";
    }

    /**
     * 保存修改密码
     */
    @PostMapping("/update")
     @ResponseBody
    public ResultVo araecountupdate(HttpServletRequest request,AreaCount areaCount){
        // 判断原来密码是否有误
    	if(areaCount.getId()>0) {
    		AreaCount areaCountold=areaCountService.getById(areaCount.getId());
    		areaCountold.setMessage_shouFabunum(areaCount.getMessage_shouFabunum());
    		areaCountold.setMessage_shouReadnum(areaCount.getMessage_shouReadnum());
    		areaCountService.save(areaCountold);
    	}else {
       	 MemberInfo agent=(MemberInfo)request.getSession().getAttribute(AdminConst.AGENT_ROLE_NAME); 
       	areaCount.setArea(agent.getArea());
    	areaCount=  areaCountService.save(areaCount);
    	}
        return ResultVoUtil.success("修改成功");
    }
}